# -*- encoding: utf-8 -*-
"""
@File    : User_register_try_line.py
@Time    : 2020/1/19 11:27 上午
@Author  : david wang
@Email   : 3063737@qq.com
@Software: PyCharm
"""
# 导入插件
import pandas as pd
import numpy as np
from pandas import DataFrame
import setting

# 定义文件路径
filename = setting.export_file+'User_register_try_line.html'
iopath = setting.resource_file+'User_order.xlsx'
outpath = setting.resource_file+'out.png'

# 读取xls文件
df = pd.DataFrame(pd.read_excel(iopath, sheet_name='Sheet1'))

# 获取数据
cols = df.columns.values.tolist()  # 获取数据头信息
da0 = [str(i)[0:10] for i in pd.Series.tolist(pd.to_datetime(df[cols[0]]))]
da1 = pd.Series.tolist(df[cols[1]])
da2 = pd.Series.tolist(df[cols[2]])

# 导入柱状图-Bar
from pyecharts.charts import Line
from pyecharts import options as opts
# 内置主题类型可查看 pyecharts.globals.ThemeType
from pyecharts.globals import ThemeType
from pyecharts.render import make_snapshot
from snapshot_phantomjs import snapshot

# from snapshot_selenium import snapshot


line = (
    Line(init_opts=opts.InitOpts(bg_color="white",
                                 theme=ThemeType.WHITE,
                                 height="600px", width="1200px"
                                 ))
        .add_xaxis(da0)  # 增加x轴
        .add_yaxis(cols[2], (da2), label_opts=opts.LabelOpts(is_show=False),
                   is_smooth=True,
                   markline_opts=opts.MarkLineOpts(data=[opts.MarkLineItem(type_="average", name="平均值")]),
                   markpoint_opts=opts.MarkPointOpts(data=[opts.MarkPointItem(type_="max", name="最大值"),
                                                           opts.MarkPointItem(type_="min", name="最大值"),
                                                           opts.MarkPointItem(type_="average", name="平均值"), ]),
                   areastyle_opts=opts.AreaStyleOpts(opacity=0.3), color="#4169E1")
        # 增加y轴，设置曲线流畅，透明度，曲线颜色
        .add_yaxis(cols[1], (da1), label_opts=opts.LabelOpts(is_show=False), is_smooth=True,
                   markline_opts=opts.MarkLineOpts(data=[opts.MarkLineItem(type_="average", name="平均值")]),
                   markpoint_opts=opts.MarkPointOpts(data=[opts.MarkPointItem(type_="max", name="最大值"),
                                                           opts.MarkPointItem(type_="min", name="最小值"),
                                                           opts.MarkPointItem(type_="average", name="平均值"), ]),
                   areastyle_opts=opts.AreaStyleOpts(opacity=0.5), color="#FF0033")
            # 增加y轴
        # 全局配置项
        .set_global_opts(
        title_opts=opts.TitleOpts(title="遏制羊毛党计划", subtitle="注册用户量和试用用户量"),
        xaxis_opts=opts.AxisOpts(
            axistick_opts=opts.AxisTickOpts(is_align_with_label=True),
            is_scale=False,
            boundary_gap=False
        ),  # 图像贴近y轴
        # legend_opts子项显示
        legend_opts=opts.LegendOpts(is_show=True),
        # 增加单位
        yaxis_opts=opts.AxisOpts(splitline_opts=opts.SplitLineOpts(is_show=True),
                                 axislabel_opts=opts.LabelOpts(formatter="{value} /个")),
        # 可以滑动的直方图控制
        #datazoom_opts=opts.DataZoomOpts(orient="vertical"),
        datazoom_opts=opts.DataZoomOpts(),
        # toolbox_opts工具栏显示
        toolbox_opts=opts.ToolboxOpts(),
        # 增加y轴分割线
        # yaxis_opts=opts.AxisOpts(splitline_opts=opts.SplitLineOpts(is_show=True))
    )

    # .set_series_opts(
    #   markpoint_opts=opts.MarkPointOpts(
    #      data=[opts.MarkPointItem(type_="max"), opts.MarkPointItem(type_="min")],
    #     symbol_size=[34, 30],
    #    label_opts=opts.LabelOpts(position="inside", color="#fff", font_size=8)
    # ),
    # label_opts=opts.LabelOpts(is_show=False),
    # markline_opts=opts.MarkLineOpts(data=[opts.MarkLineItem(type_="average")])
    # )   # 系列配置项
)
line.render(filename)

import webbrowser
webbrowser.open_new_tab('file://'+filename)
